<?php
class UserIdentity extends CUserIdentity{
	var $user = null;
	
	/**
	 * 用户验证
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate(){
		//从数据库中获取
		$this->user = UserModel::model()->get_by_attrs(array(
			'site_id' 	=> SITE_ID,
			'user_name' => CHtml::encode($this->username)
		), 'status,user_name,password'); 
		if(empty($this->user)){
			$this->errorCode = self::ERROR_USERNAME_INVALID;
		}elseif($this->user['password'] != md5($this->password . Yii::app()->params['fingerprint'])){
			$this->errorCode = self::ERROR_PASSWORD_INVALID;
		}else if($this->user['status'] == UserModel::STATUS_DISABLED){
			//用户状态失效
            $this->errorCode = self::ERROR_STATUS_INVALID;
        }else{
			//登录成功
			$this->errorCode = self::ERROR_NONE;
		}
		return !$this->errorCode;
	}
	
	const ERROR_STATUS_INVALID = 10000; //错误的用户状态
	
	public function getId(){
		return $this->user == null ? null : $this->user['_id'];
	}
}